#include "producerthread.h"

ProducerThread::ProducerThread(QObject *parent) 
    : QThread(parent), m_stopped(false)
{
}

void ProducerThread::stop()
{
    QMutexLocker locker(&m_mutex);
    m_stopped = true;
}

void ProducerThread::run()
{
    while(true) {
        {
            QMutexLocker locker(&m_mutex);
            if(m_stopped) break;
        }
        
        // 生成随机数并发射信号
        int num = QRandomGenerator::global()->bounded(100);
        emit dataProduced(num);
        
        msleep(1000);  // 每秒产生一个数
    }
}
